#!/bin/sh

MY_PATH=$(cd `dirname $0`; pwd)
cd $MY_PATH

DATE=`date +%F_%T`
PORT=$(sed -n '2p' uwsgi.xml | awk -F'[:|<]' '{print $3}')
PID=(`lsof -i:$PORT | awk '/[0-9]/{print $2}'`)
LOG_DIR=$MY_PATH/logs/django/`date +%F`

if [ -n "$PID" ];then
	for P in ${PID[@]};do
		    kill -9 $P
	done
fi

sleep 1s

if [ -e ./logs/django.log ];then
    mkdir -p $LOG_DIR
    mv ./logs/django.log $LOG_DIR/django.log.$DATE
fi

uwsgi -x $MY_PATH/uwsgi.xml

if [ -e ./logs/django.log ];then
    chmod 0644 ./logs/django.log
else
    sleep 1s
    chmod 0644 ./logs/django.log
fi
